<?php
if (!class_exists('RoleManager')) {
	/**
	 *  Manager class for user role operations
	 *	@author Alecsandru Gabriel DINU
	 *  @since Jun 27, 2008
	 * 
 	 * @Bean(id = "roleManager") 
 	 * @Property(name = "roleDao", ref="roleDao")   
	 */
	class RoleManager {
		private $dao;
		/**
		 * Sets the RoleDao
		 * @param RoleDao
		 */
		public function setRoleDao(RoleDao $dao) {
			$this->dao = $dao;	
		}		
		
		/**
		 * Saves a role in the database
		 * @param Role
		 * @access public
		 */		
		public function save(Role $role) {
			if (! ($role instanceof Role)) {
				throw new InvalidArgumentException("'role' parameter must be an instance of Role class");
			}
			Validate::notEmpty($user->getName(), "Role name is empty");
			
			$this->dao->save($role);
		}
		
		/**
		 * Gets a role base on its name or its id depending on the 
		 * parameter's type
		 * @access public
		 * @param mixed
		 * @return Role
		 */
		public function get($mixed) {
			if (is_string($mixed)) {
				return $this->dao->getByName($mixed);
			} else {
				return $this->dao->getById($mixed);
			}
		}
		
		/**
		 * Gets all the roles from the database
		 * @access public
		 * @return array
		 */
		public function getAll() {
			return $this->dao->getAll();
		}
	}
}
?>